﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Common.Web;
using CSP.Model.CSP;
using CSP.Bll;
using CSP.Bll.IObjectBlls;
using CSP.Model.CSPEnum;
using CSP.Web.Master;
using CSP.Web.UserControls;

namespace CSP.Web.MyHome.Credit
{
    public partial class List : CSPMyHome
    {
        private string _fromConsumeClass = "selected";

        private string _fromServiceClass = "";

        private string _toOtherClass = "";

        protected string FromConsumeClass
        {
            get { return _fromConsumeClass; }
        }

        protected string FromServiceClass
        {
            get { return _fromServiceClass; }
        }

        protected string ToOtherClass
        {
            get { return _toOtherClass; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //指定被选中的导航
            ((HomeMaster)Master).SelectedNavigationLink = MyHomeLeftNavigationLink.Credit;

            //用户控件初始化参数
            U_Credit.UserName = UserProvider.UserName;
            U_CSPRateList.UserName = UserProvider.UserName;

            if (!IsPostBack)
            {
                Div_FromConsume.Attributes.Add("class", "selected");
                BindRateStatisticUI();
                U_CSPRateListBindByRateBusinessType(UserControls.RateList.RateListType.FromConsume);
            }
        }

        /// <summary>
        /// 绑定评价统计UI
        /// </summary>
        private void BindRateStatisticUI()
        {
            List<RateStatistic> rateStatistics = GetRateStatisticsByUser(UserProvider.UserName);
            #region 服务者
            RateStatistic serviceGoodRating = rateStatistics.FirstOrDefault(
                r => r.CreditRating == (byte)Convert.ToInt32(CreditRatingType.Good) && r.RatingType == (byte)Convert.ToInt32(RatingType.Service));
            if (serviceGoodRating != null)
            {
                BindServiceGoodUI(serviceGoodRating.OneWeekCount, serviceGoodRating.OneMonthCount, serviceGoodRating.SixMonthCount,
                    serviceGoodRating.SixMonthAgoCount, serviceGoodRating.TotalCount.Value);
            }

            RateStatistic serviceGeneralRating = rateStatistics.FirstOrDefault(
                r => r.CreditRating == (byte)Convert.ToInt32(CreditRatingType.General) && r.RatingType == (byte)Convert.ToInt32(RatingType.Service));
            if (serviceGeneralRating != null)
            {
                BindServiceGeneralUI(serviceGeneralRating.OneWeekCount, serviceGeneralRating.OneMonthCount, serviceGeneralRating.SixMonthCount,
                   serviceGeneralRating.SixMonthAgoCount, serviceGeneralRating.TotalCount.Value);
            }

            RateStatistic serviceBadRating = rateStatistics.FirstOrDefault(
                r => r.CreditRating == (byte)Convert.ToInt32(CreditRatingType.Bad) && r.RatingType == (byte)Convert.ToInt32(RatingType.Service));
            if (serviceBadRating != null)
            {
                BindServiceBadUI(serviceBadRating.OneWeekCount, serviceBadRating.OneMonthCount, serviceBadRating.SixMonthCount,
                   serviceBadRating.SixMonthAgoCount, serviceBadRating.TotalCount.Value);
            }
            int serviceOneWeekCount = rateStatistics.Where(r => r.RatingType == (byte)Convert.ToInt32(RatingType.Service)).Sum(r => r.OneWeekCount);
            int serviceOneMonthCount = rateStatistics.Where(r => r.RatingType == (byte)Convert.ToInt32(RatingType.Service)).Sum(r => r.OneMonthCount);
            int serviceSixMonthCount = rateStatistics.Where(r => r.RatingType == (byte)Convert.ToInt32(RatingType.Service)).Sum(r => r.SixMonthCount);
            int serviceSixMonthAgoCount = rateStatistics.Where(r => r.RatingType == (byte)Convert.ToInt32(RatingType.Service)).Sum(r => r.SixMonthAgoCount);
            int serviceTotalCount = serviceSixMonthAgoCount + serviceSixMonthCount;
            BindConsumeAmountUI(serviceOneWeekCount, serviceOneMonthCount, serviceSixMonthCount, serviceSixMonthAgoCount, serviceTotalCount);

            #endregion

            #region 消费者者
            RateStatistic consumeGoodRating = rateStatistics.FirstOrDefault(
                r => r.CreditRating == (byte)Convert.ToInt32(CreditRatingType.Good) && r.RatingType == (byte)Convert.ToInt32(RatingType.Consume));
            if (consumeGoodRating != null)
            {
                BindConsumeGoodUI(consumeGoodRating.OneWeekCount, consumeGoodRating.OneMonthCount, consumeGoodRating.SixMonthCount,
                    consumeGoodRating.SixMonthAgoCount, consumeGoodRating.TotalCount.Value);
            }

            RateStatistic consumeGeneralRating = rateStatistics.FirstOrDefault(
                r => r.CreditRating == (byte)Convert.ToInt32(CreditRatingType.General) && r.RatingType == (byte)Convert.ToInt32(RatingType.Consume));
            if (consumeGeneralRating != null)
            {
                BindConsumeGeneralUI(consumeGeneralRating.OneWeekCount, consumeGeneralRating.OneMonthCount, consumeGeneralRating.SixMonthCount,
                   consumeGeneralRating.SixMonthAgoCount, consumeGeneralRating.TotalCount.Value);
            }

            RateStatistic consumeBadRating = rateStatistics.FirstOrDefault(
                r => r.CreditRating == (byte)Convert.ToInt32(CreditRatingType.Bad) && r.RatingType == (byte)Convert.ToInt32(RatingType.Consume));
            if (consumeBadRating != null)
            {
                BindConsumeBadUI(consumeBadRating.OneWeekCount, consumeBadRating.OneMonthCount, consumeBadRating.SixMonthCount,
                   consumeBadRating.SixMonthAgoCount, consumeBadRating.TotalCount.Value);
            }

            int consumeOneWeekCount = rateStatistics.Where(r => r.RatingType == (byte)Convert.ToInt32(RatingType.Consume)).Sum(r => r.OneWeekCount);
            int consumeOneMonthCount = rateStatistics.Where(r => r.RatingType == (byte)Convert.ToInt32(RatingType.Consume)).Sum(r => r.OneMonthCount);
            int consumeSixMonthCount = rateStatistics.Where(r => r.RatingType == (byte)Convert.ToInt32(RatingType.Consume)).Sum(r => r.SixMonthCount);
            int consumeSixMonthAgoCount = rateStatistics.Where(r => r.RatingType == (byte)Convert.ToInt32(RatingType.Consume)).Sum(r => r.SixMonthAgoCount);
            int consumeTotalCount = consumeSixMonthCount + consumeSixMonthAgoCount;
            BindConsumeAmountUI(consumeOneWeekCount, consumeOneMonthCount, consumeSixMonthCount, consumeSixMonthAgoCount, consumeTotalCount);

            #endregion

        }

        /// <summary>
        /// 根据用户名获取评价统计
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        private List<RateStatistic> GetRateStatisticsByUser(string userName)
        {
            List<RateStatistic> result = new List<RateStatistic>();
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
            using (IRateStatisticBll rateStatisticBll = bllFactory.GetRateStatisticBll())
            {
                var temRateStatistics = rateStatisticBll.GetRateStatisticsByUser(userName);
                if (temRateStatistics.Count != 0)
                {
                    result = temRateStatistics.ToList();
                }
            }
            return result;

        }

        protected void Btn_FromConsume_Click(object sender, EventArgs e)
        {
            Div_FromConsume.Attributes.Add("class", "selected");
            Div_FromService.Attributes.Remove("class");
            Div_ToOther.Attributes.Remove("class");

            U_CSPRateListBindByRateBusinessType(UserControls.RateList.RateListType.FromConsume);
            U_CSPRateList.RateListBind();
            Div_FromConsume.Focus();
        }

        protected void Btn_FromService_Click(object sender, EventArgs e)
        {

            Div_FromConsume.Attributes.Remove("class");
            Div_FromService.Attributes.Add("class", "selected");
            Div_ToOther.Attributes.Remove("class");

            U_CSPRateListBindByRateBusinessType(UserControls.RateList.RateListType.FromService);
            U_CSPRateList.RateListBind();
            Div_FromService.Focus();
        }

        protected void Btn_ToOther_Click(object sender, EventArgs e)
        {
            Div_FromConsume.Attributes.Remove("class");
            Div_FromService.Attributes.Remove("class");
            Div_ToOther.Attributes.Add("class", "selected");
            U_CSPRateListBindByRateBusinessType(UserControls.RateList.RateListType.RateOther);
            Div_ToOther.Focus();
        }

        /// <summary>
        /// 绑定评价列表用户控件 通过评价业务类型
        /// </summary>
        /// <param name="rateBusinessType"></param>
        private void U_CSPRateListBindByRateBusinessType(UserControls.RateList.RateListType rateBusinessType)
        {

            U_CSPRateList.RateBusinessType = rateBusinessType;
            U_CSPRateList.RateListBind();
        }

        #region 信誉统计

        #region 服务统计

        /// <summary>
        /// 绑定服务"好"评UI
        /// </summary>
        /// <param name="oneWeekCount"></param>
        /// <param name="oneMonthCount"></param>
        /// <param name="sixMonthCount"></param>
        /// <param name="sixMonthAgoCount"></param>
        /// <param name="totalCount"></param>
        private void BindServiceGoodUI(int oneWeekCount, int oneMonthCount, int sixMonthCount, int sixMonthAgoCount, int totalCount)
        {
            this.LB_ServiceOneWeekGood.Text = oneWeekCount.ToString();
            this.LB_ServiceOneMonthGood.Text = oneMonthCount.ToString();
            this.LB_ServiceSixMonthGood.Text = sixMonthCount.ToString();
            this.LB_ServiceSixMonthAgoGood.Text = sixMonthAgoCount.ToString();
            this.LB_ServiceTotalGood.Text = totalCount.ToString();
        }

        /// <summary>
        /// 绑定服务"一般"评价UI
        /// </summary>
        /// <param name="oneWeekCount"></param>
        /// <param name="oneMonthCount"></param>
        /// <param name="sixMonthCount"></param>
        /// <param name="sixMonthAgoCount"></param>
        /// <param name="totalCount"></param>
        private void BindServiceGeneralUI(int oneWeekCount, int oneMonthCount, int sixMonthCount, int sixMonthAgoCount, int totalCount)
        {
            this.LB_ServiceOneWeekGeneral.Text = oneWeekCount.ToString();
            this.LB_ServiceOneMonthGeneral.Text = oneMonthCount.ToString();
            this.LB_ServiceSixMonthGeneral.Text = sixMonthCount.ToString();
            this.LB_ServiceSixMonthAgoGeneral.Text = sixMonthAgoCount.ToString();
            this.LB_ServiceTotalGeneral.Text = totalCount.ToString();
        }

        /// <summary>
        /// 绑定服务"差"评价UI
        /// </summary>
        /// <param name="oneWeekCount"></param>
        /// <param name="oneMonthCount"></param>
        /// <param name="sixMonthCount"></param>
        /// <param name="sixMonthAgoCount"></param>
        /// <param name="totalCount"></param>
        private void BindServiceBadUI(int oneWeekCount, int oneMonthCount, int sixMonthCount, int sixMonthAgoCount, int totalCount)
        {
            this.LB_ServiceOneWeekBad.Text = oneWeekCount.ToString();
            this.LB_ServiceOneMonthBad.Text = oneMonthCount.ToString();
            this.LB_ServiceSixMonthBad.Text = sixMonthCount.ToString();
            this.LB_ServiceSixMonthAgoBad.Text = sixMonthAgoCount.ToString();
            this.LB_ServiceTotalBad.Text = totalCount.ToString();
        }

        /// <summary>
        /// 绑定服务"综合"评价UI
        /// </summary>
        /// <param name="oneWeekCount"></param>
        /// <param name="oneMonthCount"></param>
        /// <param name="sixMonthCount"></param>
        /// <param name="sixMonthAgoCount"></param>
        /// <param name="totalCount"></param>
        private void BindServiceAmountUI(int oneWeekCount, int oneMonthCount, int sixMonthCount, int sixMonthAgoCount, int totalCount)
        {
            this.LB_ConsumeOneWeekAmount.Text = oneWeekCount.ToString();
            this.LB_ServiceOneMonthAmount.Text = oneMonthCount.ToString();
            this.LB_ServiceSixMonthAmount.Text = sixMonthCount.ToString();
            this.LB_ServiceSixMonthAgoAmount.Text = sixMonthAgoCount.ToString();
            this.LB_ServiceTotalAmount.Text = totalCount.ToString();
        }
        #endregion

        #region 统计消费

        /// <summary>
        /// 绑定消费"好"评UI
        /// </summary>
        /// <param name="oneWeekCount"></param>
        /// <param name="oneMonthCount"></param>
        /// <param name="sixMonthCount"></param>
        /// <param name="sixMonthAgoCount"></param>
        /// <param name="totalCount"></param>
        private void BindConsumeGoodUI(int oneWeekCount, int oneMonthCount, int sixMonthCount, int sixMonthAgoCount, int totalCount)
        {
            this.LB_ConsumeOneWeekGood.Text = oneWeekCount.ToString();
            this.LB_ConsumeOneMonthGood.Text = oneMonthCount.ToString();
            this.LB_ConsumeSixMonthGood.Text = sixMonthCount.ToString();
            this.LB_ConsumeSixMonthAgoGood.Text = sixMonthAgoCount.ToString();
            this.LB_ConsumeTotalGood.Text = totalCount.ToString();
        }

        /// <summary>
        /// 绑定消费"一般"评价UI
        /// </summary>
        /// <param name="oneWeekCount"></param>
        /// <param name="oneMonthCount"></param>
        /// <param name="sixMonthCount"></param>
        /// <param name="sixMonthAgoCount"></param>
        /// <param name="totalCount"></param>
        private void BindConsumeGeneralUI(int oneWeekCount, int oneMonthCount, int sixMonthCount, int sixMonthAgoCount, int totalCount)
        {
            this.LB_ConsumeOneWeekGeneral.Text = oneWeekCount.ToString();
            this.LB_ConsumeOneMonthGeneral.Text = oneMonthCount.ToString();
            this.LB_ConsumeSixMonthGeneral.Text = sixMonthCount.ToString();
            this.LB_ConsumeSixMonthAgoGeneral.Text = sixMonthAgoCount.ToString();
            this.LB_ConsumeTotalGeneral.Text = totalCount.ToString();
        }

        /// <summary>
        /// 绑定消费"差"评价UI
        /// </summary>
        /// <param name="oneWeekCount"></param>
        /// <param name="oneMonthCount"></param>
        /// <param name="sixMonthCount"></param>
        /// <param name="sixMonthAgoCount"></param>
        /// <param name="totalCount"></param>
        private void BindConsumeBadUI(int oneWeekCount, int oneMonthCount, int sixMonthCount, int sixMonthAgoCount, int totalCount)
        {
            this.LB_ConsumeOneWeekBad.Text = oneWeekCount.ToString();
            this.LB_ConsumeOneMonthBad.Text = oneMonthCount.ToString();
            this.LB_ConsumeSixMonthBad.Text = sixMonthCount.ToString();
            this.LB_ConsumeSixMonthAgoBad.Text = sixMonthAgoCount.ToString();
            this.LB_ConsumeTotalBad.Text = totalCount.ToString();
        }

        /// <summary>
        /// 绑定消费"综合"评价UI
        /// </summary>
        /// <param name="oneWeekCount"></param>
        /// <param name="oneMonthCount"></param>
        /// <param name="sixMonthCount"></param>
        /// <param name="sixMonthAgoCount"></param>
        /// <param name="totalCount"></param>
        private void BindConsumeAmountUI(int oneWeekCount, int oneMonthCount, int sixMonthCount, int sixMonthAgoCount, int totalCount)
        {
            this.LB_ConsumeOneWeekAmount.Text = oneWeekCount.ToString();
            this.LB_ConsumeOneMonthAmount.Text = oneMonthCount.ToString();
            this.LB_ConsumeSixMonthAmount.Text = sixMonthCount.ToString();
            this.LB_ConsumeSixMonthAgoAmount.Text = sixMonthAgoCount.ToString();
            this.LB_ConsumeTotalAmount.Text = totalCount.ToString();
        }
        #endregion


        #endregion
    }
}